﻿namespace Pibby.Core
{
	using System;
	using System.Reflection;

	public static class EnumExtensions
	{
		public static string ToDescription(this Enum descriptiveEnum)
		{
			Type enumType = descriptiveEnum.GetType();

			MemberInfo[] memInfo = enumType.GetMember(descriptiveEnum.ToString());

			if (memInfo != null && memInfo.Length > 0)
			{
				object[] attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

				if (attributes != null && attributes.Length > 0)
				{
					return ((DescriptionAttribute)attributes[0]).Text;
				}
			}

			return descriptiveEnum.ToString();
		}
	}
}
